<?php

class TrainerProfileReviewController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'TrainerProfileReview'),
        ));
    }

    public function actioncreate() {
        $model = new TrainerProfileReview;
        if (isset($_POST['text'])) {
            $loginuser = getLoginUserId();
            if (!empty($loginuser) && ($loginuser != $_POST['userId'])) {
                $model->setAttribute('reviews_text', $_POST['text']);
                $model->setAttribute('from_user_id', $loginuser);
                $model->setAttribute('create_time', SkillConfig::CreateDateTime());

                if ($_POST['userType'] == 'backend') {
                    $model->setAttribute('backend_user_id', $_POST['userId']);
                    $reviewsUser = $model->findByAttributes(array('to_user_id' => $_POST['userId'], 'backend_user_id' => $loginuser));

                    if ($reviewsUser <> "" && ($reviewsUser->backend_user_id == $loginuser)) {
                        echo 'againsameuser';
                        die;
                    }
                } else {
                    $model->setAttribute('to_user_id', $_POST['userId']);
                    $reviewsUser = $model->findByAttributes(array('to_user_id' => $_POST['userId'], 'from_user_id' => $loginuser));

                    if ($reviewsUser <> "" && ($reviewsUser->from_user_id == $loginuser)) {
                        echo 'againsameuser';
                        die;
                    }
                }
                if ($model->save(FALSE)) {
                    echo Message::TRAINER_SUCCESS_MSG;
                } die;
            }


            if ($loginuser == $_POST['userId']) {
                echo 'fail';
            } else {
                echo 'redirect';
            }
        }
    }

}